//! 网络模块
//! 
//! 提供PostgreSQL协议和HTTP API支持

pub mod postgres;
pub mod postgres_v2;
pub mod http;

use crate::{Error, Result};

/// 网络服务器配置
#[derive(Debug, Clone)]
pub struct NetworkConfig {
    /// PostgreSQL端口
    pub postgres_port: u16,
    /// HTTP端口
    pub http_port: u16,
    /// 绑定地址
    pub bind_address: String,
    /// 最大连接数
    pub max_connections: usize,
}

impl Default for NetworkConfig {
    fn default() -> Self {
        Self {
            postgres_port: 5432,
            http_port: 8080,
            bind_address: "127.0.0.1".to_string(),
            max_connections: 1000,
        }
    }
}

/// 网络服务器
#[derive(Debug)]
pub struct NetworkServer {
    config: NetworkConfig,
}

impl NetworkServer {
    /// 创建新的网络服务器
    pub fn new(config: NetworkConfig) -> Self {
        Self { config }
    }
    
    /// 启动服务器
    pub async fn start(&self) -> Result<()> {
        tracing::info!("启动网络服务器");
        tracing::info!("PostgreSQL端口: {}", self.config.postgres_port);
        tracing::info!("HTTP端口: {}", self.config.http_port);
        
        // 这里是简化实现，实际需要启动多个服务
        Ok(())
    }
    
    /// 停止服务器
    pub async fn stop(&self) -> Result<()> {
        tracing::info!("停止网络服务器");
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    
    #[test]
    fn test_network_config_default() {
        let config = NetworkConfig::default();
        assert_eq!(config.postgres_port, 5432);
        assert_eq!(config.http_port, 8080);
        assert_eq!(config.bind_address, "127.0.0.1");
    }
    
    #[tokio::test]
    async fn test_network_server_creation() {
        let config = NetworkConfig::default();
        let server = NetworkServer::new(config);
        
        // 测试启动和停止
        server.start().await.unwrap();
        server.stop().await.unwrap();
    }
}
